package thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * Created by yl on 2019/10/11
 */
public class PerfectStopThread {

    public static final ExecutorService executorService =
            new ThreadPoolExecutor(2,
                    2,
                    60L, TimeUnit.SECONDS,
                    new SynchronousQueue<Runnable>(),
                    Executors.defaultThreadFactory(), new RejectedExecutionHandler() {
                @Override
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    try {
                        executor.getQueue().put(r);
                    } catch (InterruptedException e) {
//                        e.printStackTrace();
                        Thread.currentThread().interrupt();
                    }
                }
            });

    public static void main(String[] args) {
        List<Thread> threadList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (Thread.currentThread().isInterrupted() == false){
                        executorService.submit(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    Thread.currentThread().sleep(2 * 1000);
                                    System.out.println(Thread.currentThread().getName() + " is working");
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                        if(Thread.currentThread().isInterrupted()){
                            break;
                        }
                        /*try {
                            Thread.currentThread().sleep(1000);
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        }*/
                    }
                }
            });
            t.setName("thread" + i);
            t.start();
            threadList.add(t);
        }

        threadList.forEach( t -> {
            System.out.println(t.isAlive()+" "+t.getName());
        });
        try {
            Thread.currentThread().sleep(1 * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        threadList.forEach( t -> {
            t.interrupt();
        });
        for (int i = 0; i < threadList.size(); ) {
            if( threadList.get(i).isAlive()){
                System.out.println(threadList.get(i).getName() + " is still running");
                /*try {
                    Thread.currentThread().sleep(1 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }*/
                continue;
            }else {
                i++;
            }
        }
        threadList.forEach( t -> {
            System.out.println(t.isAlive()+" "+t.getName());
        });

//        executorService.shutdown();
//        System.exit(0);
    }
}
